'use strict';
const db = uniCloud.database()
const dbCmd = db.command
const $ = dbCmd.aggregate
exports.main = async (event, context) => {
	//event为客户端上传的参数
	console.log('event : ', event)
	let ids = [226]
	let res = await db.collection('uni-id-users').where({
		id: dbCmd.in(ids),
		role: 'member'
	}).get()
	console.log('res', res)
	for (var i = 0; i < res.data.length; i++) {
		let m = res.data[i]
		await db.collection("uni-id-users").doc(m._id).update({
			// shareGrade: 0,
			vipInfo: dbCmd.set(null)
		})
		let accountRes = await db.collection('t3ds-account').where({
			userId: m._id
		}).update({
			balance: 1000000,
			total: 1000000,
			frozen: 0,
			settle: 0
		})
		console.log('accountRes', accountRes)
		let shareOrderRes = await db.collection('t3ds-share-order').where({
			userId: m._id 
		}).remove()
		console.log('shareOrderRes', shareOrderRes)
		let orderRes = await db.collection('t3ds-order').where({
			user_id: m._id 
		}).get()
		console.log('orderRes', orderRes)
		if (orderRes.data.length) {
			let orderIds = orderRes.data.map(item => {
				return item._id
			})
			let ticketRes = await db.collection('t3ds-ticket-code').where({
				orderId: dbCmd.in(orderIds)
			}).remove()
			console.log('ticketRes', ticketRes)
			let delOrderRes = await db.collection('t3ds-order').where({
				_id: dbCmd.in(orderIds)
			}).remove()
			console.log('delOrderRes', delOrderRes)
		}
		
		let accountLogRes = await db.collection('t3ds-account-log').where({
			userId: m._id
		}).remove()
		console.log('accountLogRes', accountLogRes)
		// let relationRes = await db.collection("t3ds-share-expert-relation").where({
		// 	userId: m._id
		// }).get()
		// if (relationRes.data.length) {
		// 	let relation = relationRes.data[0]
		// 	let removeRelationRes = await db.collection("t3ds-share-expert-relation").doc(relation._id).remove()
		// 	console.log('removeRelationRes', removeRelationRes)
		// 	let updateRelationRes = await db.collection("t3ds-share-expert-relation").where({
		// 		parentId: relation._id
		// 	}).update({
		// 		parentId: ''
		// 	})
		// 	console.log('updateRelationRes', updateRelationRes)
		// }
		let cacheRes = await db.collection('file-cache')
			.where({
				srcUrl: new RegExp(`goods-poster\\w*_${m.id}_\\w*`)
			})
			.limit(25)
			.get()
		var fileIDs = cacheRes.data.map(item => {
			return item.fileID
		})
		let posterRes = await db.collection("file-cache")
			.where({
				srcUrl: new RegExp(`^poster_${m._id}`)
			})
			.limit(25)
			.get()
		fileIDs = fileIDs.concat(posterRes.data.map(item => {
			return item.fileID
		}))
		console.log('fileIDs', fileIDs)
		if (fileIDs.length) {
			let delRes = await uniCloud.deleteFile({
				fileList: fileIDs
			})
			console.log('delRes', delRes)
		}
		let delGoodsPosterCacheRes = await db.collection('file-cache')
			.where({
				srcUrl: new RegExp(`^goods-poster\\w*_${m.id}_\\w*`)
			}).remove()
		console.log('delGoodsPosterCacheRes', delGoodsPosterCacheRes)
		let delPosterCacheRes = await db.collection('file-cache')
			.where({
				srcUrl: new RegExp(`^poster\\w*_${m._id}_\\w*`)
			}).remove()
		console.log('delPosterCacheRes', delPosterCacheRes)
		let delTicktGiftRes = await db.collection('t3ds-ticket-gift')
			.where({
				receiverId: m._id
			})
			.remove()
		console.log('delTicktGiftRes', delTicktGiftRes)
		console.log('reset finished')
	}
	//返回数据给客户端
	return event
};
